~ chicken-core (chicken-5) /manual/Module (chicken flonum)


  1[[tags: manual]]
  2[[toc:]]
  3
  4== Module (chicken flonum)
  5
  6Because CHICKEN supports a full numeric tower, operations can
  7sometimes incur a subtantial overhead to simply detect the type of numbers
  8you're passing in.  When you know you're definitely dealing only with
  9flonums, you can choose to use flonum-specific operations to avoid
 10this overhead.
 11
 12This is purely a performance hack.  You might want to consider adding
 13[[Types|type annotations]] instead, this often gives the same
 14performance boost without having to rewrite all numeric operators in
 15your code.
 16
 17=== Arithmetic floating-point operations
 18
 19<procedure>(fp+ X Y)</procedure>
 20<procedure>(fp- X Y)</procedure>
 21<procedure>(fp* X Y)</procedure>
 22<procedure>(fp/ X Y)</procedure>
 23<procedure>(fp*+ X Y Z)</procedure>
 24<procedure>(fpgcd X Y)</procedure>
 25<procedure>(fpneg X)</procedure>
 26<procedure>(fpmin X Y)</procedure>
 27<procedure>(fpmax X Y)</procedure>
 28<procedure>(fp= X Y)</procedure>
 29<procedure>(fp> X Y)</procedure>
 30<procedure>(fp< X Y)</procedure>
 31<procedure>(fp>= X Y)</procedure>
 32<procedure>(fp<= X Y)</procedure>
 33<procedure>(fpfloor X)</procedure>
 34<procedure>(fpceiling X)</procedure>
 35<procedure>(fptruncate X)</procedure>
 36<procedure>(fpround X)</procedure>
 37<procedure>(fpsin X)</procedure>
 38<procedure>(fpcos X)</procedure>
 39<procedure>(fptan X)</procedure>
 40<procedure>(fpasin X)</procedure>
 41<procedure>(fpacos X)</procedure>
 42<procedure>(fpatan X)</procedure>
 43<procedure>(fpatan2 X Y)</procedure>
 44<procedure>(fpsinh X)</procedure>
 45<procedure>(fpcosh X)</procedure>
 46<procedure>(fptanh X)</procedure>
 47<procedure>(fpasinh X)</procedure>
 48<procedure>(fpacosh X)</procedure>
 49<procedure>(fpatanh X)</procedure>
 50<procedure>(fplog X)</procedure>
 51<procedure>(fpexp X)</procedure>
 52<procedure>(fpexpt X Y)</procedure>
 53<procedure>(fpsqrt X)</procedure>
 54<procedure>(fpabs X)</procedure>
 55<procedure>(fpinteger? X)</procedure>
 56
 57Arithmetic floating-point operations.
 58
 59In safe mode, these procedures throw a type error when given non-float
 60arguments. In unsafe mode, these procedures do not check their
 61arguments. A non-flonum argument in unsafe mode can crash the
 62application. {{fp*+}} implements fused multiply-add {{(X * Y) + Z}}.
 63
 64Note: {{fpround}} uses the rounding mode that your C library
 65implements, which is usually different from R5RS.
 66
 67== Flonum limits
 68
 69<constant>maximum-flonum</constant><br>
 70<constant>minimum-flonum</constant><br>
 71<constant>flonum-radix</constant><br>
 72<constant>flonum-epsilon</constant><br>
 73<constant>flonum-precision</constant><br>
 74<constant>flonum-decimal-precision</constant><br>
 75<constant>flonum-maximum-exponent</constant><br>
 76<constant>flonum-minimum-exponent</constant><br>
 77<constant>flonum-maximum-decimal-exponent</constant><br>
 78<constant>flonum-minimum-decimal-exponent</constant><br>
 79
 80Platform-specific flonum limits.
 81
 82<procedure>(flonum-print-precision [PRECISION])</procedure>
 83
 84Gets and sets the number of significant digits printed for a floating-point
 85number. {{PRECISION}} must be a positive {{fixnum}}.  Returns
 86the setting that was previously in effect.
 87
 88The default print precision is 15 on nearly all systems, and 7
 89on the rare system on which the {{double}} type is only single-precision.
 90
 91'''Note:''' To ensure read/write invariance for ''all'' floating-point
 92numbers, you must increase print precision from 15 to 17 (or from 7 to
 939).  For example:
 94
 95 > (define a (expt 2 -53))
 96 > (define b (+ a (* 2 (expt 10 -32))))
 97 > (eqv? a b)
 98 #f
 99 > (flonum-print-precision 15)
100 > (cons a b)
101 (1.11022302462516e-16 .
102  1.11022302462516e-16)            ;; same printed representation
103 > (flonum-print-precision 17)
104 > (cons a b)
105 (1.1102230246251565e-16 .
106  1.1102230246251568e-16)          ;; differs in last place
107
108On the downside, this will result in unnecessarily precise
109representations of many numbers:
110
111 > (flonum-print-precision 17)
112 > 0.1
113 0.10000000000000001
114
115The maximum number of decimal digits required to uniquely represent
116all floating-point numbers of a certain precision is given by the
117formula {{ceil(1+N*log10(2))}}, where N is the number of bits of
118precision; for double-precision, {{N=53}}.
119
120
121---
122Previous: [[Module (chicken fixnum)]]
123
124Next: [[Module (chicken foreign)]]
Trap